'''
Author: Donald duck tang5722917@163.com
Date: 2024-03-29 15:10:08
LastEditors: Donald duck tang5722917@163.com
LastEditTime: 2024-04-08 16:44:32
Description: 
Copyright (c) 2024 by Donald duck email: tang5722917@163.com, All Rights Reserved.
'''
from transitions import Machine, State


class Token_status :
    transitions = [
        {'trigger': 'next', 'source': 'INITIAL', 'dest': 'node'},
        {'trigger': 'next', 'source': 'node', 'dest': 'opt', 'conditions' : 'is_full_node'},
        {'trigger': 'vmagn_match', 'source': 'opt', 'dest': 'vmagn'},
        {'trigger': 'magn_match', 'source': 'vmagn', 'dest': 'vunit'},
        {'trigger': 'next_space', 'source': ['vmagn','vunit'], 'dest': 'opt'},
        {'trigger': 'next_control', 'source': 'INITIAL', 'dest': 'opt'},
        {'trigger': 'next_space', 'source': '*', 'dest': 'None'},
    ]
    
    def say_hello(self):
        pass

    def say_goodbye(self):
        pass

    def set_node_number(self,num):
        self.number = num
        
    def node_counter(self):
            self.counter += 1
        
    def enter_node_event(self):
        self.lexer.begin('node')
        
    def is_full_node(self):
        if(self.number == self.counter):
            return True
        else:
            return False      
                    
    def enter_vmagn_event(self):
        self.lexer.begin('vmagn')
        
    def enter_vunit_event(self):
        self.lexer.begin('vunit')
    
    def enter_opt_event(self):
        self.lexer.begin('opt')
        
    def __init__(self,lexer,Debug_enable,logging) :
        states = [
            State(name='INITIAL', on_enter='say_hello', on_exit='say_goodbye'),
            State(name='node', on_enter='enter_node_event', on_exit='say_goodbye'),
            State(name='vmagn', on_enter='enter_vmagn_event', on_exit='say_goodbye'),
            State(name='vunit', on_enter='enter_vunit_event', on_exit='say_goodbye'),
            State(name='opt', on_enter='enter_opt_event', on_exit='say_goodbye'),
        ]
        self.machine = Machine(model=self, states=states, transitions=Token_status.transitions, initial='INITIAL')
        self.Debug_enable = Debug_enable
        self.logging = logging
        self.logging.getLogger('transitions').setLevel(logging.WARNING)
        self.lexer = lexer
        self.counter = 0
        self.number  = 0
        
